package rejjie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import rejjie.common.CustomException;
import rejjie.entity.Setmeal;
import rejjie.entity.SetmealDish;
import rejjie.mapper.SetmealMapper;
import rejjie.service.SetmealDishService;
import rejjie.service.SetmealService;

import javax.security.auth.callback.LanguageCallback;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     *    * 不光要山出 setmeal表
     *      * 同时要根绝setmealID删除对应的SetmealDish表中的数据
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除  只能删除没有进行售卖的套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        // 查询正在售卖的套餐的数量
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(setmealLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖，不能进行删除");
        }
        this.removeByIds(ids);

        // 删除SetmealDish表
        // remove 传入的是queryWrapper, 而对于removeByid删除 传入的是id
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }
}
